

#include <iostream>
#include <thread>

#include "../user.pb.h"
#include "mrpcapplication.h"
#include "rpcprovider.h"
#include "../../src/include/jsonUtils.h"
#include "mrpcchannel.h"

class UserService : public fixbug::UserServiceRpc, public JsonService
{

public:
    std::shared_ptr<bool> login(const std::string &username, const std::string &password)
    {
        std::cout << "login start username = " << username << ". pass = " << password << std::endl;
        if ("zs" == username && "123" == password)
        {
            std::cout << "logind success" << std::endl;
            return std::make_shared<bool>(true);
        }
        return std::make_shared<bool>(false);
    }

    void login(::google::protobuf::RpcController *controller,
               const ::fixbug::LoginRequest *request,
               ::fixbug::LoginResponse *response,
               ::google::protobuf::Closure *done)
    {
        const std::string &username = request->username();
        const std::string &password = request->password();

        std::shared_ptr<bool> result = login(username, password);
        bool r = result.get();
        response->set_success(r);
        response->set_message(r ? "success" : "faile");

        done->Run();
    }
    void login(RpcJsonParam &jsonParam)
    {
        std::shared_ptr<bool> result = login(jsonParam.requestJson["username"], jsonParam.requestJson["password"]);
        bool r = result.get();

        jsonParam.response.success = r;
        jsonParam.response.message = r ? "success" : "faile";
        std::cout << "login end, response success  = " << jsonParam.response.success << ". message = " << jsonParam.response.message << std::endl;
    }

    void call(std::string methodName, RpcJsonParam &param) override
    {
        std::cout << "UserService call start, methodName = " << methodName << std::endl;
        std::cout << "param = " << param.requestJson.dump() << std::endl;

        if ("login" == methodName)
        {
            login(param);
            return;
        }
        
    }
};
RpcProvider::StrategyRegistry<UserService> regis("UserServiceRpc");

int main(int argc, char **argv)
{

    MrpcApplication::init(argc, argv);

    RpcProvider provider;

    provider.notifyService(new UserService());

    provider.run();

    std::cout << "end .. " << std::endl;

    // provider.setMessageCallback(nullptr, nullptr, muduo::Timestamp::now());

    return 0;
};
